package com.ykun.shiro.chapter6.service;

import com.ykun.shiro.chapter6.dao.UserDao;
import com.ykun.shiro.chapter6.dao.UserDaoImpl;
import com.ykun.shiro.chapter6.entity.User;

import java.util.Set;

/**
 * Created by yangkun on 2017/8/29.
 */
public class UserServiceImpl implements UserService {

    private UserDao dao = new UserDaoImpl();
    private PasswordHelper passwordHelper = new PasswordHelper();

    public User createUser(User user) {
        passwordHelper.encryptPassword(user);
        return dao.createUser(user);
    }

    public void changePassword(Long userId, String newPassword) {
        User user = dao.findOne(userId);
        user.setPassword(newPassword);
        passwordHelper.encryptPassword(user);
        dao.updateUser(user);
    }

    public void correlationRoles(Long userId, Long... roleIds) {
        dao.correlationRoles(userId, roleIds);
    }

    public void uncorrelationRoles(Long userId, Long... roleIds) {
        dao.uncorrelationRoles(userId, roleIds);
    }

    public User findByUsername(String username) {
        return dao.findByUsername(username);
    }

    public Set<String> findRoles(String username) {
        return dao.findRoles(username);
    }

    public Set<String> findPermissions(String username) {
        return dao.findPermissions(username);
    }
}
